#coding: utf-8
#encoding: utf-8

class EstateController < ApplicationController
  def index
    @oneroom = Oneroom.find(:all, :order => "price", :order => "date")
    @oneroom.reverse!

    @bookmark = Bookmark.all
    @expand_bookmarks = Array.new
    for i in @bookmark
      @expand_bookmarks << Oneroom.find(i.flat_id)
    end
    
  end

  def add_to_bookmarks
    bookmark_ = { "flat_id" => params[:id]}
    bmarks = Bookmark.all
    bmarks.each do |i|
      if i.flat_id.to_i == params[:id].to_i
        redirect_to_index
        return
      end
    end
    @bookmark = Bookmark.new(bookmark_)
    if @bookmark.save
      redirect_to_index
    end
    
  end

  def remove_bookmark
    bmarks = Bookmark.all
    in_id = 0
    bmarks.each do |i|
      if i.flat_id.to_i == params[:id].to_i
        in_id = i.id
      end
    end
    @bookmark = Bookmark.find(in_id)
    @bookmark.destroy
    redirect_to_index
  end

  def show
    @oneroom = Oneroom.find(params[:id])
    @comment = Comment.new
  end

  def add_comment
    @comment = Comment.new(params[:comment])
  end

  private

  def redirect_to_index
    redirect_to :action => 'index' 
  end
  

end
